#include "stdafx.h"
#include "ProjectInfo.h"

#include "ToolConfigFile.h"

ProjectInfo::ProjectInfo(void)
{
}

ProjectInfo::~ProjectInfo(void)
{
}

void ProjectInfo::Clear()
{
	projectName.Empty();
	projectLocation.Empty();
	bakprjFile.Empty();

	fullBackupPath.Empty();
	incBackupPath.Empty();
	difBackupPath.Empty();
	difincBackupPath.Empty();

	fullBackupInfoList.clear();
	incBackupInfoList.clear();
	difBackupInfoList.clear();
	difincBackupInfoList.clear();
	selectedBackup = NULL;
	currentBackup.Init();

	recoveryPath.Empty();

	selectedFileList.clear();
	selectedDirList.clear();

	//m_CurSelTabFrm = 0;
	//m_CurSelTabFrmOfBackup = 0;
	//m_CurSelTabRecoveryList = 0;

	autoCycle = 0;
	autoHour = 0;
	autoMin = 0;

	isInclude = FALSE;
	backupProgress = 0;

	this->selectedBackup = NULL;
}

BOOL ProjectInfo::SaveProject()
{
	this->bakprjFile.Format(_T("%s\\%s.bakprj"), this->projectLocation, this->projectName);
	return SaveConfigFile(this->bakprjFile, 
		this->projectName,
		this->fullBackupPath, 
		this->incBackupPath,
		this->difBackupPath,
		this->difincBackupPath,
		this->autoCycle,
		this->autoHour,
		this->autoMin,
		this->isAutoBackupOnce,
		this->currentBackup.fileList);
}

BackupInfo* ProjectInfo::GetlatestFullBackup()
{
	BackupInfo* p = NULL;
	std::list<BackupInfo>::iterator ite;
	for(ite = this->fullBackupInfoList.begin(); ite != this->fullBackupInfoList.end(); ++ ite) {
		if(p == NULL || p->backupTime < (*ite).backupTime) {
			p = &(*ite);
		}
	}

	return p;
}

BackupInfo* ProjectInfo::GetlatestIncBackup()
{
	BackupInfo* p = NULL;
	std::list<BackupInfo>::iterator ite;
	for(ite = this->incBackupInfoList.begin(); ite != this->incBackupInfoList.end(); ++ ite) {
		if(p == NULL || p->backupTime < (*ite).backupTime) {
			p = &(*ite);
		}
	}

	return p;
}

BackupInfo* ProjectInfo::GetlatestFullOrIncBackup()
{
	BackupInfo* p1 = this->GetlatestFullBackup();
	BackupInfo* p2 = this->GetlatestIncBackup();
	if(p1 == NULL) {
		return p2;
	}
	else if(p2 == NULL) {
		return p1;
	}
	else if(p1->backupTime > p2->backupTime) {
		return p1;
	}
	else {
		return p2;
	}
}

BOOL ProjectInfo::GetHistoryBackupList(std::list<BackupInfo*>& historyBackupList, BackupInfo* backupInfo)
{
	if(backupInfo == NULL) {
		historyBackupList.push_front(&this->currentBackup);
	}
	else {
		historyBackupList.push_front(backupInfo);
	}
	if(this->AddHistoryBackupListRecursively(historyBackupList)) {
		historyBackupList.pop_back();
		return TRUE;
	}
	else {
		return FALSE;
	}
}

BOOL ProjectInfo::AddHistoryBackupListRecursively(std::list<BackupInfo*>& historyBackupList)
{
	BackupInfo* depended;
	
	CString dependedName = historyBackupList.front()->dependedBackup;
	if(dependedName.IsEmpty()) {
		return TRUE;
	}

	depended = FindBackupInList(dependedName, this->fullBackupInfoList);
	if(depended == NULL)
		depended = FindBackupInList(dependedName, this->incBackupInfoList);
	if(depended == NULL)
		depended = FindBackupInList(dependedName, this->difBackupInfoList);
	if(depended == NULL)
		depended = FindBackupInList(dependedName, this->difincBackupInfoList);
	
	if(depended == NULL) {
		return FALSE;
	}
	else {
		historyBackupList.push_front(depended);
		return AddHistoryBackupListRecursively(historyBackupList);
	}
}

BackupInfo* ProjectInfo::FindBackupInList(CString name, std::list<BackupInfo>& list)
{
	std::list<BackupInfo>::iterator ite;
	for(ite = list.begin(); ite != list.end(); ++ ite) {
		if(name == (*ite).backupName) {
			return &(*ite);
		}
	}
	return NULL;
}
